package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.entity.TimeList;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public void add(ReservationDto reservationDto) {
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservationMapper.add(reservation);
    }

    @Override
    public PageResponse<ReservationVo> getByPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<ReservationVo> page = reservationMapper.getByPage(status);
        PageResponse<ReservationVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public void cancel(Long id) {
        reservationMapper.cancel(id);
    }

    @Override
    public List<Map<String, String>> countByTime(Long time) {
        Date date = new Date(time);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(date);
        List<Map<String, String>> list = new ArrayList<>();
        TimeList t = new TimeList();
        for (String s : t.getTimeList()) {
            Map<String, String> result = new HashMap<>();
            String str = formattedDate + " " + s;
            result.put("time", str);
            result.put("count", Integer.toString(6 - reservationMapper.countByTime(str)));
            list.add(result);
        }
        return list;
    }
}
